/*
 * MessagesCreate.java
 *
 * Created on November 15, 2008, 8:08 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package jollylengkono.skripsi.Messages.action;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import jollylengkono.skripsi.Messages.form.MessagesCreateForm;
import jollylengkono.skripsi.dao.MembersDao;
import jollylengkono.skripsi.dao.MessagesDao;
import jollylengkono.skripsi.model.Members;
import jollylengkono.skripsi.model.Messages;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author JollkY
 */
public class MessagesCreate extends Action {
    private String FORWARD;
    private int STATE;
    
    /** Creates a new instance of MessagesCreate */
    public MessagesCreate() {
    }
    
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        HttpSession sess = request.getSession(true);
        Members sender = (Members) sess.getAttribute("memberSessObject");
        MessagesCreateForm msgForm = (MessagesCreateForm) form;
        Messages msg = new Messages();
        msg.setAcceptDate(""); //GENERATE HERE
        msg.setMessageStatus(msgForm.getMessageStatus());
        List recipients = new ArrayList();
        for(String idMsg : msgForm.getIdRecipients()) {
            recipients.add(new MembersDao().read(Long.parseLong(idMsg)));
        }
        msg.setRecipients(recipients);
        msg.setSender(sender);
        msg.setSubject(msgForm.getSubject());
        msg.setText(msgForm.getText());
        this.STATE = new MessagesDao().send(msg);
        if (this.STATE==1) {
            this.FORWARD="success";
        } else {
            this.FORWARD="fail";
        }
        return mapping.findForward(FORWARD);
    }
    
}
